// pages/sendDemand/sendDemand.js
var network = require("../../utils/request.js")
var util = require('../../utils/util.js')
var nowDate = util.formatTime(new Date);
const app = getApp();

const date = new Date()
const years = []
const months = []
const days = []

years.push(date.getFullYear());
for (let i = 1; i <= 12; i++) {
  months.push(i)
}


Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeList:[],
    typeIndex:null,
    hospitalList:[],
    hospitalIndex:0,
    sectionList:[],
    sectionIndex:null,
    doctorList:[],
    doctorIndex:0,
    noData:false,
    chooseTimeFirst: '',
    chooseTimeSecond: '',
    doctorLineNum:0,
    showTextarea:true,
    hospital:false,
    schoolname:'',
    isRz:false,//是否实名认证
    schoolName:'',//医院名字
    categoryId:null,//业务id
    hospitalId:'',//医院id
    departmentId: null,//科室id
    doctorLevel: null,//医师级别
    doctorNames: null,//医生名字
    remark: null,//备注
    identityType:null,//署名方式
    time:false,
    years: years,
    year: date.getFullYear(),
    months: months,
    days1: '',
    days2: '',
    days3:'',//时间段
    time1:'',
    time2:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     this.getServiceType();
     this.getDoctorLevel();
     this.getIdentityTypeName();

    let that = this;
    let year = date.getFullYear();
    let month = 1;
    let day = 1;
    var monthDaySize;
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
      monthDaySize = 31;
    } else if (month == 4 || month == 6 || month == 9 || month == 11) {
      monthDaySize = 30;
    } else if (month == 2) {
      // 计算是否是闰年,如果是二月份则是29天
      if ((year - 2000) % 4 == 0) {
        monthDaySize = 29;
      } else {
        monthDaySize = 28;
      }
    };
    console.log(monthDaySize)
    this.setData({
      days1: monthDaySize,
      days2: monthDaySize,
      time1: month + "月"  + (day > 10 ? day : '0' + day),
      time2: month + "月" + (day > 10 ? day : '0' + day)
    })
  },

  setSchoolName:function(e){
    this.setData({
      schoolName: e.detail.value
    })
  },
  searchSchool:function(){
    if (this.data.schoolName == "") {
      return
    }
    this.getSchool(this.data.schoolName)
  },
  selTime1:function(){
      this.setData({
        time:true,
        showTextarea: false,
      })
  },
  selTime2: function () {
    this.setData({
      timeInput: true,
      showTextarea: false,
      showTextarea: false,
    })
  },
  cancleTime:function(){
      this.setData({
        time: false,
        showTextarea: true,
        timeInput:false,
        showTextarea: true,
      })
  },
  setTime1:function(){
      this.setData({
        time: false,
        showTextarea: true,
        chooseTimeFirst:this.data.time1+"至"+ this.data.time2
      })
  },
  setTimeCon:function(e){
      this.setData({
        days3: e.detail.value
      })
  },
  setTime2:function(){
      this.setData({
        timeInput: false,
        chooseTimeSecond:this.data.days3
      })
  },
  bindChange1(e) {
    let that = this;
    let year = date.getFullYear();
    let month = e.detail.value[0]+1;
    let day = e.detail.value[1] + 1;
    var monthDaySize;
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
      monthDaySize = 31;
    } else if (month == 4 || month == 6 || month == 9 || month == 11) {
      monthDaySize = 30;
    } else if (month == 2) {
      // 计算是否是闰年,如果是二月份则是29天
      if ((year - 2000) % 4 == 0) {
        monthDaySize = 29;
      } else {
        monthDaySize = 28;
      }
};
     this.setData({
       days1:monthDaySize,
       time1: month + "月" + (day > 10 ? day : '0' + day)
     })
  },
  bindChange2(e) {
    let that = this;
    let year = date.getFullYear();
    let month = e.detail.value[0] + 1;
    let day = e.detail.value[1] + 1;
    var monthDaySize;
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
      monthDaySize = 31;
    } else if (month == 4 || month == 6 || month == 9 || month == 11) {
      monthDaySize = 30;
    } else if (month == 2) {
      // 计算是否是闰年,如果是二月份则是29天
      if ((year - 2000) % 4 == 0) {
        monthDaySize = 29;
      } else {
        monthDaySize = 28;
      }
    };
    console.log(monthDaySize)
    this.setData({
      days2: monthDaySize,
      time2: month + "月"+ (day > 10 ? day : '0' + day)
    })
  },
  //查询署名方式
  getIdentityTypeName: function () {
    var that = this;
    network.requestLoading('POST', app.globalData.requestUrl + 'getDemandTypeAPI',
      {
      }, '', function (res) {
        //如果未实名认证
        if (!that.data.isRz){
          that.setData({
            identityType: res.data[0].code
          })
        }
        else{
          that.setData({
            identityType: res.data[1].code + "," + res.data[2].code
          })
        }
      })
  },

  //获取医院名字
  getSchool: function () {
    var that = this;
    network.requestLoading('POST', app.globalData.requestUrl + 'getHospitalInfoAPI',
      {
          name:that.data.schoolName
      }, '', function (res) { 
          that.setData({
            hospitalList: res.data,
            noData: res.data.length==0?true:false,
          })
      })
  },
  //获取医院下科室名字
  getDepartmentInfoByHospital: function (id) {
    var that = this;
    network.requestLoading('POST', app.globalData.requestUrl + 'getDepartmentInfoByHospitalIdAPI',
      {
        hospitalId: id
      }, '', function (res) {
        that.setData({
          sectionList: res.data,
          departmentId: res.data[0].id
        })
       
      })
  },

  //获取业务类型接口
  getServiceType:function(){
    var that = this;
    network.requestLoading('POST', app.globalData.requestUrl + 'getServiceCategoryClassAInfoAPI',
      {
       
      }, '', function (res) {
        for(let i in res.data){
          that.data.typeList.push(res.data[i])
        }
        that.setData({
          typeList: that.data.typeList
        })
         
      })
  },
  //获取医师级别数据
  getDoctorLevel: function () {
    var that = this;
    network.requestLoading('POST', app.globalData.requestUrl + 'getDoctorLevelAPI',
      {

      }, '', function (res) {
        for (let i in res.data) {
          that.data.doctorList.push(res.data[i])
        }
        that.setData({
          doctorList: that.data.doctorList,
          doctorLevel: that.data.doctorList[0].code
        })
      })
  },

  //选择医院
  selHospital:function(e){
    let obj = e.currentTarget.dataset.obj;
    this.setData({
      hospital: false,
      schoolName: obj.name,
      schoolname: obj.name,
      hospitalId: obj.id,
    })
    this.getDepartmentInfoByHospital(obj.id);
  },
  //设置医院
  setHospital:function(e){
      this.setData({
        hospital:true,
        showTextarea: false,
        hospitalList:[],
        showTextarea:false,
        noData:false,
      })
  },
  close: function () {
    this.setData({
      hospital: false,
      showTextarea: true,
    })
  },
  cancle:function(){
      wx.navigateBack({})
  },
  //发布需求
  saveDemand: function () {
    var that = this;
    let time = [];
    if (that.data.hospitalId == ''){
        wx.showToast({
          title: '请选择医院',
          icon: "none"
        })
        return
    }
    if (that.data.chooseTimeFirst == '' && that.data.chooseTimeSecond == '') {
      wx.showToast({
        title: '请选择就诊时间',
        icon: "none"
      })
      return
    }
    
    time.push(this.data.chooseTimeFirst, this.data.chooseTimeSecond)
    time = time.join(',');
    network.requestLoading('POST', app.globalData.requestUrl + 'addConsumerDemandInfoAPI',
      {
        consumerId: app.globalData.consumerid,
        categoryId: that.data.categoryId,
        hospitalId: that.data.hospitalId,
        departmentId: that.data.departmentId,
        doctorLevel: that.data.doctorLevel,
        doctorNames: that.data.doctorNames,
        time: time,
        remark: that.data.remark,
        identityType: that.data.identityType,
      }, '', function (res) {
        //res就是我们请求接口返回的数据
        wx.showToast({
          title: '发布成功',
          icon:"success",
          duration:3000
        })
        setTimeout(function(){
          wx.switchTab({
            url: '/pages/mineService/mineService',
          })
        },3000)
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  bindTypeChange:function(e){
      console.log(this.data.typeList)
      this.setData({
        typeIndex: e.detail.value,
        categoryId: this.data.typeList[e.detail.value].id
      })
  },
  bindSectionChange:function(e){
    if (this.data.sectionList.length == 0){
      wx.showToast({
        title: '请先选择医院',
        icon:"none"
      })
      return
    }
    this.setData({
      sectionIndex: e.detail.value,
      departmentId: this.data.sectionList[e.detail.value].id
    })
  },
  bindHospitalChange:function(e){
    this.setData({
      hospitalIndex: e.detail.value
    })
  },
  bindDoctorChange:function(e){
    this.setData({
      doctorIndex: e.detail.value,
      doctorLevel: this.data.doctorList[e.detail.value].code
    })
  },  
  //医生名字
  setDoctorName:function(e){
    this.setData({
      doctorNames: e.detail.value
    })
  },
  bindTimeFirstChange:function(e){
    console.log(e)
    this.setData({
      chooseTimeFirst:e.detail.value
    })
  },
  bindTimeSecondChange:function(e){
    this.setData({
      chooseTimeSecond: e.detail.value
    })
  },
  reduce:function(){
    this.setData({
      doctorLineNum: --this.data.doctorLineNum
    })
  },
  increase:function(){
    if (this.data.doctorLineNum==5) return false
    this.setData({
      doctorLineNum: ++this.data.doctorLineNum
    })
  },
  setTextArea:function(e){
      this.setData({
        remark:e.detail.value
      })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})